package org.budo.beetl.format;

import java.util.Date;

import org.beetl.core.Format;
import org.budo.support.lang.util.StringUtil;
import org.budo.time.Time;

/**
 * @author lmw
 * @see org.beetl.ext.format.DateFormat
 */
public class BudoBeetlDateFormat implements Format {
    private static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Object format(Object value, String pattern) {
        if (value == null) {
            return null;
        }

        if (StringUtil.isEmpty(pattern)) {
            pattern = PATTERN_DEFAULT;
        }

        if (Date.class.isAssignableFrom(value.getClass())) {
            return Time.when((Date) value).toString(pattern);
        }

        if (value.getClass() == Long.class) {
            return Time.when((Long) value).toString(pattern);
        }

        throw new RuntimeException("#40 BudoBeetlDateFormat error, value=" + value + ", type=" + value.getClass());
    }
}
